package com.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.dao.UserDao;
import com.model.User;



public class Util {

	
	// 获取当前月份
	public static String getYuefen() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
		Date date = new Date();
		return sdf.format(date.getTime());
	}
	
	// 获取当前系统时间
	public static String getTime() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date = new Date();
		return sdf.format(date.getTime());
	}
	
	// 获取当前日期
	public static String getRiqi() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date = new Date();
		return sdf.format(date.getTime());
	}
	
	

	// 获取当前系统时间
	public static String getTime2() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		Date date = new Date();
		return sdf.format(date.getTime());
	}

	// 上传文件/复制文件。
	public static void copyFile(File src, File dst) {
		try {
			int BUFFER_SIZE = 16 * 1024;
			InputStream in = null;
			OutputStream out = null;
			try {
				in = new BufferedInputStream(new FileInputStream(src),
						BUFFER_SIZE);
				out = new BufferedOutputStream(new FileOutputStream(dst),
						BUFFER_SIZE);
				byte[] buffer = new byte[BUFFER_SIZE];
				for (int byteRead = 0; (byteRead = in.read(buffer)) > 0;) {
					out.write(buffer, 0, byteRead);
				}

			} finally {
				if (null != in) {
					in.close();
				}
				if (null != out) {
					out.close();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}



	// 初始化系统
	public static void init(HttpServletRequest request) {
		WebApplicationContext app = WebApplicationContextUtils
				.getWebApplicationContext(request.getSession()
						.getServletContext());
		UserDao userDao = (UserDao) app.getBean("userDao");
		User user = userDao
				.selectBean(" where username='admin' and deletestatus=0  ");
		if (user == null) {
			user = new User();
			user.setPassword("111111");
			user.setRole(0);
			user.setUsername("admin");
			userDao.insertBean(user);
		}
	}

	// 获取当前系统时间
	public static Date parseTime( String str ) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
          
		
		    return sdf.parse(str);
	}
	public static Date parseTime1( String str ) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
          
		
		    return sdf.parse(str);
	}
	 //获取当前周数
	public static int parserweek() throws ParseException{
		 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		    Date date = new Date();
		   Calendar calendar = Calendar.getInstance();
		   calendar.setFirstDayOfWeek(Calendar.MONDAY);
		   calendar.setTime(date);
		  
		return calendar.get(Calendar.WEEK_OF_YEAR);
		
	}
	
	//获取最后预约的周数
	public static int parserweek1(String today) throws ParseException{
		// String today = "2010-01-11";
		  SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		  Date date = format.parse(today);
		  Calendar calendar = Calendar.getInstance();
		  calendar.setFirstDayOfWeek(Calendar.MONDAY);
		  calendar.setTime(date);
		 // System.out.println(calendar.get(Calendar.WEEK_OF_YEAR));
		return calendar.get(Calendar.WEEK_OF_YEAR);
		
	}
	
	//得到当前日期是星期几
	public static String getweekname(String time){
		final String dayNames[] = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五",
		"星期六" };
		
		SimpleDateFormat sdfInput = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		Calendar calendar = Calendar.getInstance();
		Date date = new Date();
		try {
		date = sdfInput.parse(time);
		} catch (ParseException e) {
		e.printStackTrace();
		}
		calendar.setTime(date);
		int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK)-1;
		if(dayOfWeek<0)dayOfWeek=0;
		
		return dayNames[dayOfWeek];
	}
    
	//Sting2016-06-07 08:00到String2016-06-07
	public static String getshortdate(String str) throws ParseException{

	    //	String str="2016-06-07 08:00";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
	      Date dateDate=sdf.parse(str);
		  SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		   String dateString = formatter.format(dateDate);
		 //  System.out.println(dateString);
		return dateString;
	}
	
	//计算两个时间段相差多少分钟
	public static long shijiancha(String a,String b) throws ParseException{
		 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
	        long m = sdf.parse(b).getTime() - sdf.parse(a).getTime();
	      // long c=  m/(1000*60*60);
	        long c=  m/(1000*60);
	    return c;
	}
}
